package com.ehais.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;


import com.ehais.modbuswifi.R;

import java.util.List;

public class NewScanBatteryMonomerAdapter extends ArrayAdapter<Double> {

    public NewScanBatteryMonomerAdapter(@NonNull Context context, int resource, @NonNull List<Double> objects) {
        super(context, resource, objects);
    }

    //每个子项被滚动到屏幕内的时候会被调用
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        Double value = getItem(position);//得到当前项的 Fruit 实例
        View view;
        //新增一个内部类 ViewHolder，用于对控件的实例进行缓存
        ViewHolder viewHolder;
        if (convertView==null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.adapter_new_scan_battery_monomer,parent,false);
            viewHolder = new ViewHolder();
            viewHolder.tvName = view.findViewById(R.id.tvName);
            viewHolder.tvValue = view.findViewById(R.id.tvValue);
            view.setTag(viewHolder);//将 viewHolder 存储在 view 中
        }else{
            view=convertView;
            viewHolder= (ViewHolder) view.getTag();//重新获取 viewHolder
        }
        viewHolder.tvName.setText(""+(position + 1)+"号");
        viewHolder.tvValue.setText(value.toString());

        return view;
    }

    private class ViewHolder {
        TextView tvName;
        TextView tvValue;
    }


}
